package com.gp.myController;

import com.gp.mvcframework.GPAutowired;
import com.gp.mvcframework.GPController;
import com.gp.mvcframework.GPRequestMapping;
import com.gp.mvcframework.GPRequestParam;
import com.gp.myService.IDemoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@GPController
@GPRequestMapping("/demo")
public class MyController{

    @GPAutowired
    private IDemoService demoService;

    @GPRequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response, @GPRequestParam("name") String name, String[] addr){
//    public void query(@GPRequestParam("name") String name, String addr, int age){

//        String result = "My name is " + name;
        String s = demoService.get(name);

        try (PrintWriter writer = response.getWriter();){
            writer.write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @GPRequestMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @GPRequestParam("a") Integer a, @GPRequestParam("b") Integer b){

        String result = a + "+" + b + "=" + (a+b);
        System.out.println(result);

        try (PrintWriter writer = response.getWriter();){
            writer.write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Class<MyController> clazz = MyController.class;
        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            System.out.println(method.getName());
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                System.out.println(parameter.getName());
            }


            System.out.println("======================");
        }

    }

}